// import config from "../config/index";
import utils from "../utils/index";
class Http {
    get(params: any) {
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        var header: any = params.header
        header['Authorization'] = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJvcGVuSWQiOiJvemtLTTRuQzZDamkyVFpLLWJIcGNrZXVBR1NJIiwiZXhwIjoxNzEwMzE4OTY0NDQ2fQ.Wg2ohxttMhKjCzJKkjMX0P_xwap_C42WcgXCT525i1M"
        return new Promise((success, error) => {

            wx.request({
                method: 'GET',
                url: utils.checkUrl() + params.url, //仅为示例，并非真实接口地址。
                data: params.data,
                header: params.header,
                success: (res) => {
                    success(this.HttpInterceptor(res))
                },
                fail: (err) => {
                    error(err)
                }
            });
        })
    }

    post(params: any) {
        wx.showLoading({
            title: '加载中',
            mask: true

        });
        var header: any = params.header
        header['Authorization'] = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJvcGVuSWQiOiJvemtLTTRuQzZDamkyVFpLLWJIcGNrZXVBR1NJIiwiZXhwIjoxNzEwMzE4OTY0NDQ2fQ.Wg2ohxttMhKjCzJKkjMX0P_xwap_C42WcgXCT525i1M"
        return new Promise((success, error) => {
            wx.request({
                method: 'POST',
                url: utils.checkUrl() + params.url, //仅为示例，并非真实接口地址。
                data: params.data,
                header: header,
                success: (res) => {
                    success(this.HttpInterceptor(res))
                },
                fail: (err) => {
                    error(err)
                }
            });
        })
    }

    HttpInterceptor(result: any) {
        wx.hideLoading({
        }) 
        try {
            var data:any = result.data || {}
            data.statusCode = result.statusCode
            switch (result.data.errCode) {
                case 601:
                    wx.showToast({ 
                        title:result.data.errMgs
                    })
                    break;
                default:
                    return data
                    break;
            }
        } catch (e) {
            console.error(e)
        }

    }

}

export default new Http();
